// 房间结构、功能通用函数
export default class RoomStructureExtension extends Room {
    // Link自动运行函数A型1号
    public linkWork_A1(mode:number):void
    {
        /* 根据mode确定模式，初级为 1 */
        if (mode == 1)
        {
            var link1:StructureLink; var link2:StructureLink
            // 1 模式下只允许单线传送，资源点的Link传给中央Link
            var storage_ = new RoomPosition(this.memory.center_x,this.memory.center_y,this.name).findClosestByRange(FIND_STRUCTURES,{filter:{
                structureType: STRUCTURE_STORAGE
            }})
            if (storage_) 
            {
                link1 = storage_.pos.findClosestByRange(FIND_STRUCTURES,{filter:{structureType:STRUCTURE_LINK}}) as StructureLink
                if (link1)
                link1.register_center_link()
            }
            // 远离控制器的link
            var source_ = this.findFarSource()
            if  (source_)
            {
                link2 = source_.pos.findClosestByRange(FIND_STRUCTURES,{filter:{structureType:STRUCTURE_LINK}}) as StructureLink
                if (link2)
                link2.register_source_link()
            }
            
        }
        /* 根据this.memory里的相关内容确定是否存在该id */
        // 如果只存在该两种link的情况下
        if (this.memory.source_link && this.memory.center_link)
        {
            var souce_link = Game.getObjectById(this.memory.source_link) as StructureLink
            var center_link = Game.getObjectById(this.memory.center_link) as StructureLink
            souce_link.transferEnergy(center_link)
        }
    }


    // Tower自动运行函数A型2号
    public towerWork_A1():void{

        if (!this.memory.StructureIdData) return
        if (this.memory.StructureIdData.NtowerID)
        {
            var Ntower = Game.getObjectById(this.memory.StructureIdData.NtowerID as string) as StructureTower
            if (!Ntower){delete this.memory.StructureIdData.NtowerID}
            else Ntower.work_A1()
        }
        if (this.memory.StructureIdData.OtowerID && this.memory.StructureIdData.OtowerID.length > 0)
        {
            for (var i of this.memory.StructureIdData.OtowerID)
            {
                var Otower = Game.getObjectById(i as string) as StructureTower
                if (!Otower){delete this.memory.StructureIdData.NtowerID}
                else Otower.work_A2()
            }
        }
    }
}